package com.fuze.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuze.common.Advice;
import com.fuze.common.CustomException;
import com.fuze.domain.dto.DataGridView;
import com.fuze.domain.dto.UserDTO;
import com.fuze.domain.model.User;
import com.fuze.domain.vo.LoginFormVO;
import com.fuze.domain.vo.UserQueryVO;
import com.fuze.mapper.UserMapper;
import com.fuze.service.UserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author sepc_
 * @date 4/7/2021 16:02
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    final RedisTemplate<String, Object> redisTemplate;

    final UserMapper userMapper;

    @Override
    public String login(LoginFormVO loginFormVO) {
        String token;
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(qw.eq(User::getUsername, loginFormVO.getUsername()));
        if (user == null) {
            throw new CustomException(Advice.USER_NOT_EXIST);
        }
        if (!user.getPassword().equals(SecureUtil.md5(loginFormVO.getPassword()))) {
            throw new CustomException(Advice.WRONG_PASSWORD);
        }
        token = IdUtil.simpleUUID();
        redisTemplate.opsForValue().set(token, user, 24, TimeUnit.HOURS);
        return token;
    }

    @Override
    public Boolean existByUsername(String username) {
        return userMapper.existByUsername(username);
    }

    @Override
    public Boolean existByMarketMembersName(String marketMembersName) {
        return userMapper.existByMarketMembersName(marketMembersName);
    }

    @Override
    public int register(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setPassword(SecureUtil.md5(userDTO.getPassword()));
        return userMapper.insert(user);
    }

    @Override
    public int deleteByIds(Integer[] userIds) {
        List<Integer> ids = Arrays.asList(userIds);
        return this.userMapper.deleteBatchIds(ids);
    }

    @Override
    public DataGridView listUserForPage(UserQueryVO userQueryVO) {
        // 构建分页条件
        Page<User> page = new Page<>(userQueryVO.getPageNum(), userQueryVO.getPageSize());
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq(StringUtils.isNotBlank(userQueryVO.getLocation()), User.COL_LOCATION, userQueryVO.getLocation())
                .eq(StringUtils.isNotBlank(userQueryVO.getType()), User.COL_TYPE, userQueryVO.getType())
                .eq(StringUtils.isNotBlank(userQueryVO.getBindType()), User.COL_BIND_TYPE, userQueryVO.getBindType())
                .select(User.COL_ID, User.COL_ID,
                        User.COL_USERNAME, User.COL_MARKET_MEMBERS_NAME,
                        User.COL_LOCATION, User.COL_ENTERPRISE_LEGAL_PERSON_NAME,
                        User.COL_LEGAL_REPRESENTATIVE_NAME, User.COL_ELECTRICITY_CONSUMPTION_ADDRESS,
                        User.COL_TYPE, User.COL_BIND_TYPE);
        this.userMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public User findUserById(Integer userId) {
        User user = this.userMapper.selectById(userId);
        if (user == null) {
            throw new CustomException(Advice.USER_NOT_EXIST);
        }
        // 密码置空
        user.setPassword(null);
        return user;
    }

    @Override
    public int update(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return userMapper.updateById(user);
    }
}
